package com.mfangsoft.zhuangjialong.integration.order.service.impl;



import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mfangsoft.zhuangjialong.common.context.UserContext;
import com.mfangsoft.zhuangjialong.common.model.Page;
import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.integration.order.mapper.BaseOrderMapper;
import com.mfangsoft.zhuangjialong.integration.order.model.BaseOrder;
import com.mfangsoft.zhuangjialong.integration.order.model.OrderExplain;
import com.mfangsoft.zhuangjialong.integration.order.model.OrderGift;
import com.mfangsoft.zhuangjialong.integration.order.model.OrderInfo;
import com.mfangsoft.zhuangjialong.integration.order.model.ProductInfo;
import com.mfangsoft.zhuangjialong.integration.order.service.IOrderService;
import com.mfangsoft.zhuangjialong.integration.user.model.UserEntity;



@Service
public class OrderServiceimpl implements IOrderService {
	
	
	private static final Logger LOGGER = LoggerFactory.getLogger(OrderServiceimpl.class); 
	
	
	@Autowired
	private BaseOrderMapper baseOrderMapper;

	@Override
	public Page<Map<String,Object>> selectOrderPage(Page<Map<String,Object>> page) {
		Map<String,Object> map=(Map<String, Object>) page.getParam();
		UserEntity user = UserContext.getCurrentUser();
		if(user!=null){
			if(map!=null){
				map.put("level", user.getUser_level());
				map.put("shopId",user.getId());
			}else{
				map=new HashMap<String, Object>();
				map.put("level", user.getUser_level());
				map.put("shopId",user.getId());
			}
		}
		page.setParam(map);
		List<Map<String,Object>> selectOrderPage = baseOrderMapper.selectOrderPage(page);
//		if(selectOrderPage!=null && selectOrderPage.size()>0){
			page.setData(selectOrderPage);
//		}
		return page;
	}


	@Override
	public OrderInfo selectOrderDetail(Integer orderId) {
		OrderInfo orderDetail = baseOrderMapper.selectOrderDetail(orderId);
		if(orderDetail!=null){
			List<ProductInfo> selectOrderProduct = baseOrderMapper.selectOrderProduct(orderId);
			orderDetail.setProductInfoList(selectOrderProduct);
			String selectFiliale = baseOrderMapper.selectFiliale(orderDetail.getShopId());
			orderDetail.setFiliale(selectFiliale);
			List<Map<String,Object>> orderGift = baseOrderMapper.selectOrderGift(orderId);
			StringBuffer prizes=new StringBuffer();
			List<String> list=new ArrayList<String>();
			if(orderGift!=null && orderGift.size()>0){
				for(Map<String,Object> map:orderGift){
					String string = (String) map.get("gift");
					if(string!=null){
						String[] split = string.split(",");
						for(int x=0;x<split.length;x++){
							list.add(split[x]);
						}
					}
				}
			}
			if(list!=null && list.size()>0){
				for(int y=0;y<list.size();y++){
					if(y==0){
						if("".equals(list.get(y))){
							
						}else{
							prizes.append(list.get(y));
						}
					}else{
						if(prizes.length()>0){
							prizes.append("/"+list.get(y));
						}else{
							prizes.append(list.get(y));
						}
					}
				}
			}
			orderDetail.setActivityDetail(orderGift);
			orderDetail.setPrizes(prizes.toString());
			/*List<Map<String,Object>> activityNumRule = baseOrderMapper.selectActivityNumRule(orderDetail.getActivityId());
			List<Map<String,Object>> activityPresentRule = baseOrderMapper.selectActivityPresentRule(orderDetail.getActivityId());
			orderDetail.setActivityNumRule(activityNumRule);
			orderDetail.setActivityPresentRule(activityPresentRule);*/
			List<String> orderGiving = baseOrderMapper.selectOrderGiving(orderId);
			if(orderGiving!=null && orderGiving.size()>0){
				StringBuffer sb=new StringBuffer();
				for(String s:orderGiving){
					if(sb.length()>0){
						sb.append("/"+s);
					}else{
						sb.append(s);
					}
				}
				orderDetail.setPrize(sb.toString());
			}
		}
		return orderDetail;
	}


	@Override
	public void updateOrderStatus(OrderInfo order) {
		if(order.getOrderState()==4){
			order.setCloseWay(2);
		}
		baseOrderMapper.updateOrderStatus(order);
	}


	@Override
	public void addOrderExplain(OrderExplain OrderExplain) {
		UserEntity user = UserContext.getCurrentUser();
		if(user!=null){
			OrderExplain explain = baseOrderMapper.selectExplain(Integer.valueOf(user.getId().toString()));
			if(explain!=null){
				OrderExplain.setShopId(Integer.valueOf(user.getId().toString()));
				baseOrderMapper.updateExplain(OrderExplain);
			}else{
				OrderExplain.setShopId(Integer.valueOf(user.getId().toString()));
				baseOrderMapper.addOrderExplain(OrderExplain);
			}
		}
	}


	@Override
	public void updateExplain(OrderExplain OrderExplain) {
		baseOrderMapper.updateExplain(OrderExplain);
	}


	@Override
	public OrderExplain selectExplain() {
		UserEntity user = UserContext.getCurrentUser();
		if(user!=null){
			OrderExplain explain = baseOrderMapper.selectExplain(Integer.valueOf(user.getId().toString()));
			return explain;
		}
		return null;
		
	}
	
	@Override
	public void updateOrder(OrderInfo order) {
		if (order != null) {
			baseOrderMapper.updateOrder(order);
			baseOrderMapper.deleteOrderProduct(order.getOrderId());
			List<ProductInfo> productInfoList = order.getProductInfoList();
			if (productInfoList != null && productInfoList.size() > 0) {
				for (ProductInfo product : productInfoList) {
					product.setOrderId(order.getOrderId());
					baseOrderMapper.addOrderProducts(product);
				}
			}
			List<Map<String,Object>> num = order.getNum();
			if(num!=null && num.size()>0){
				for(Map<String,Object> map:num){
					Integer itemId = Integer.valueOf(map.get("itemId").toString());
					Integer num1 = Integer.valueOf(map.get("num").toString());
					OrderGift orderGift=new OrderGift();
					orderGift.setItemId(itemId);
					orderGift.setOrderId(order.getOrderId());
					Integer selectNum = baseOrderMapper.selectNum(orderGift);
					for(int x=0;x<num1-selectNum;x++){
						OrderGift orderGift1=new OrderGift();
						String luckyDate = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
						String luckyCode = "HDQ" + luckyDate + String.valueOf(Math.round(Math.random() * 1000));
						orderGift1.setActivityId(order.getActivityId());
						orderGift1.setCode(luckyCode);
						orderGift1.setCustomerId(order.getCustomerId());
						orderGift1.setStatus(0);
						orderGift1.setItemId(itemId);
						baseOrderMapper.addOrderGift(orderGift1);
					}
				}
			}
		}
	}


	@Override
	public Map<String, Object> selectOrderTotalMoney(Map<String, Object> map) {
		UserEntity user = UserContext.getCurrentUser();
		if(user!=null){
			if(map!=null){
				map.put("level", user.getUser_level());
				map.put("shopId",user.getId());
			}else{
				map=new HashMap<String, Object>();
				map.put("level", user.getUser_level());
				map.put("shopId",user.getId());
			}
		}
		Map<String, Object> totalMoney = baseOrderMapper.selectOrderTotalMoney(map);
		return totalMoney;
	}

}
